package com.day1014;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class MyServer {
    public static void main(String[] args) throws IOException {
        //  socket  服务端
        ServerSocket serverSocket = new ServerSocket(9999);
        //  监听当前电脑IP端口9999
        //   端口有55535
        //   前1000个最好不要乱用
        Socket socket = serverSocket.accept();
        InputStream inputStream = socket.getInputStream();
        byte[] temp = new byte[1024];
        StringBuffer stringBuffer = new StringBuffer();
        while (inputStream.read(temp) != -1){
            String str = new String(temp);
            stringBuffer.append(str);
        }
        OutputStream outputStream = socket.getOutputStream();
        String str = "客户端，我是服务端，我收到消息了";
        outputStream.write(str.getBytes());
        outputStream.flush();
        inputStream.close();
        outputStream.close();
        socket.close();
        System.out.println("服务端接收到了客户端的数据：" + stringBuffer);


    }
}
